/*
 * @(#)ObjectUtil.java        1.0 2009-8-11
 *
 * Copyright (c) 2007-2009 Shanghai Handpay IT, Co., Ltd.
 * 16/F, 889 YanAn Road. W., Shanghai, China
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of 
 * Shanghai Handpay IT Co., Ltd. ("Confidential Information").  
 * You shall not disclose such Confidential Information and shall use 
 * it only in accordance with the terms of the license agreement you 
 * entered into with Handpay.
 */

package com.yellowpage.white.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Random;

import org.apache.commons.beanutils.PropertyUtilsBean;

/**
 * 对象工具类
 * 
 * @author Administrator
 * 
 */
public class ObjectUtil {
	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 *            字符串
	 * @return 是否为空
	 */
	public static boolean isNull(String str) {
		return (str == null || "".equals(str.trim()));
	}

	/**
	 * 将以元为单位的金额转化为以分为单位的金额
	 * 
	 * @param amount
	 *            以元为单位的金额
	 * @return 以分为单位的金额
	 */
	public static int moneyConvertYuan2Fen(double amount) {
		if (amount > 0)
			return (int) (amount * 100 + 0.5d);
		else
			return (int) (amount * 100 - 0.5d);
	}

	/**
	 * 将以分为单位的金额转化为以元为单位的金额
	 * 
	 * @param amount
	 *            以分为单位的金额
	 * @return 以元为单位的金额
	 */
	public static double moneyConvertFen2Yuan(int amount) {
		return (double) (amount / 100.00d);
	}

	/**
	 * 对象属性拷贝 不拷贝非null的属性
	 * 
	 * @param dest
	 *            目标对象
	 * @param src
	 *            源对象
	 */
	public static void copyProperties(Object dest, Object src) {
		PropertyUtilsBean util = new PropertyUtilsBean();
		PropertyDescriptor[] srcProps = util.getPropertyDescriptors(src
				.getClass());
		for (int i = 0; i < srcProps.length; i++) {
			Method srcMethod = srcProps[i].getReadMethod();
			Object srcValue = null;
			try {
				srcValue = srcMethod.invoke(src);
			} catch (Exception ex) {
				continue;
			}
			if (srcValue == null) {
				continue;
			}
			Class type = srcProps[i].getPropertyType();
			String merthodName = "set"
					+ srcProps[i].getName().substring(0, 1).toUpperCase()
					+ srcProps[i].getName().substring(1);
			Method destMethod = null;
			try {
				destMethod = dest.getClass().getMethod(merthodName, type);
			} catch (Exception ex) {
				continue;
			}
			if (destMethod == null) {
				continue;
			}
			try {
				destMethod.invoke(dest, srcValue);
			} catch (Exception ex) {
				continue;
			}
		}
	}

	/**
	 * 将整数转化为定长的字符串
	 * 
	 * @param value
	 *            整数
	 * @param strLength
	 *            字符串长度
	 * @return
	 */
	public static String convertInt2String(Integer value, int strLength) {
		if (value == null) {
			return null;
		}

		String result = value.toString();
		if (result.length() < strLength) {
			int count = strLength - result.length();
			for (int i = 0; i < count; i++) {
				result = "0" + result;
			}
		}

		return result;
	}

	/**
	 * 产生随机数字符串
	 * 
	 * @param digitNumber
	 *            字符串的长度
	 * @param modeNum
	 *            模数
	 * @return
	 */
	public static String createRandomDigitStr(int digitNumber, int modeNum) {
		StringBuffer buffer = new StringBuffer();
		Random rand = new Random(System.currentTimeMillis());
		for (int i = 0; i < digitNumber; i++) {
			buffer.append(rand.nextInt(modeNum));
		}
		return buffer.toString();
	}

	/**
	 * 判断两个对象是否相同或equals
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean areEqual(Object a, Object b) {
		if (a == b)
			return true;
		else if (a != null && a.equals(b))
			return true;
		else if (b != null && b.equals(a))
			return true;
		else
			return false;
	}

	/**
	 * 判断两个字符串是否相同或equals或者都isNull
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean areEqual(String a, String b) {
		if (areEqual((Object) a, (Object) b))
			return true;
		else if (isNull(a) && isNull(b))
			return true;
		else
			return false;
	}

	// 编码转换
	public static String ISOtoUTF(String iso) {
		String gb;
		try {
			if (null == iso || ("").equals(iso)) {
				return "";
			} else {
				iso = iso.trim();
				gb = new String(iso.getBytes("ISO-8859-1"), "UTF-8");
				return gb;
			}
		} catch (Exception e) {
			System.err.print("编码转换错误：" + e.getMessage());
			return "";
		}
	}
}
